/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sunsite.dao;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.persistence.Query;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import sunsite.po.DownloadHistory;
import sunsite.po.Resource;
import sunsite.po.ResourceCatelog;
import sunsite.po.ResourceTp;
import sunsite.po.ResourceType;
import sunsite.tools.Pagination;

/**
 *
 * @author Administrator
 */
public class FileDaoImpl extends HibernateDaoSupport implements FileDao {

    public List<ResourceTp> getAllResourceTp() {
        return getHibernateTemplate().find("from ResourceTp");
    }

    public List<ResourceCatelog> getAllResourceCatelog() {
        return getHibernateTemplate().find("from ResourceCatelog");
    }

    public List<ResourceType> getAllResourceType() {
        return getHibernateTemplate().find("from ResourceType");
    }

    public ResourceCatelog getResourceCatelog(int catid) {
        return (ResourceCatelog) getHibernateTemplate().get(ResourceCatelog.class, catid);
    }

    public List<ResourceCatelog> getResourceCatelogList(final int tpid) {
        return (List<ResourceCatelog>) getHibernateTemplate().execute(new HibernateCallback() {

            public Object doInHibernate(Session arg0) throws HibernateException, SQLException {
                return arg0.createQuery("from ResourceCatelog rg where rg.resourceTp.tpId = :tpid").setParameter("tpid", tpid).list();
            }
        });

    }

    public List<ResourceType> getResourceTypeList(final int catid) {
        return (List<ResourceType>) getHibernateTemplate().execute(new HibernateCallback() {

            public Object doInHibernate(Session arg0) throws HibernateException, SQLException {
                return arg0.createQuery("from ResourceType rg where rg.resourceCatelog.catId = :catid").setParameter("catid", catid).list();
            }
        });
    }

    public void addResourceType(ResourceType resourceType) {
        getHibernateTemplate().save(resourceType);
    }

    public void addResource(Resource resource) {
        getHibernateTemplate().save(resource);
    }

    public void modifyResource(Resource resource) {
        getHibernateTemplate().update(resource);
    }

    public void addDownloadHistory(DownloadHistory downloadHistory) {
        getHibernateTemplate().save(downloadHistory);
    }
    public ResourceType getResourceType(int typeid) {
        return (ResourceType) getHibernateTemplate().get(ResourceType.class, typeid);
    }

    public List<ResourceCatelog> getNullResourceCatelog() {
        return getHibernateTemplate().find("from ResourceCatelog");
    }

    public List<Resource> getAllResource() {
        return getHibernateTemplate().find("from Resource");
    }

    public void deleteResource(String resid) {
        Resource resource = getResource(resid);
        if (resource != null) {
            getHibernateTemplate().delete(resource);
        }
    }

    public Resource getResource(String resid) {
        return (Resource) getHibernateTemplate().get(Resource.class, resid);
    }

    public List<Resource> getResourceList(final Pagination pagination) {
        String hql = "from Resource as n";
        this.getHibernateTemplate().setCacheQueries(true);
        int totalRecord = ((Long) this.getSession().createQuery(
                "select count(*) " + hql).uniqueResult()).intValue();
        int totalPage = totalRecord % pagination.getSize() == 0 ? totalRecord / pagination.getSize() : totalRecord / pagination.getSize() + 1;
        pagination.setTotalRecord(totalRecord);
        pagination.setTotalPage(totalPage);
        hql += " order by n.resId desc";
        final String hql1 = hql;
        return (List<Resource>) this.getHibernateTemplate().execute(
                new HibernateCallback() {

                    public Object doInHibernate(Session session)
                            throws HibernateException, SQLException {
                        org.hibernate.Query query = session.createQuery(hql1);
                        query.setFirstResult((pagination.getCurrentPage() - 1) * pagination.getSize());
                        query.setMaxResults(pagination.getSize());
                        return query.list();
                    }
                });

    }

    public List<Resource> getResourceList(final Pagination pagination, final Map map) {
        String hql = "from Resource as n where n.resourceType.resourceCatelog.resourceTp.tpId like :rtp " +
                "and n.resourceType.resourceCatelog.catId like :rcatelog " +
                "and n.resourceType.typeId like :rtype";
        this.getHibernateTemplate().setCacheQueries(true);        
        org.hibernate.Query query = this.getSession().createQuery("select count(*) " + hql);
        query.setString("rtp", map.get("rtp").toString());
        query.setString("rcatelog", map.get("rcatelog").toString());
        query.setString("rtype", map.get("rtype").toString());
        int totalRecord = ((Long) query.uniqueResult()).intValue();
        int totalPage = totalRecord % pagination.getSize() == 0 ? totalRecord / pagination.getSize() : totalRecord / pagination.getSize() + 1;
        pagination.setTotalRecord(totalRecord);
        pagination.setTotalPage(totalPage);
        hql += " order by n.resId desc";
        final String hql1 = hql;
        return (List<Resource>) this.getHibernateTemplate().execute(
                new HibernateCallback() {

                    public Object doInHibernate(Session session)
                            throws HibernateException, SQLException {
                        org.hibernate.Query query = session.createQuery(hql1);

                        query.setString("rtp", map.get("rtp").toString());
                        query.setString("rcatelog", map.get("rcatelog").toString());
                        query.setString("rtype", map.get("rtype").toString());
                        
                        query.setFirstResult((pagination.getCurrentPage() - 1) * pagination.getSize());
                        query.setMaxResults(pagination.getSize());
                        return query.list();
                    }
                });

    }


}
//        Iterator it = map.keySet().iterator();
//        while (it.hasNext()) {
//            Object key = it.next();
//            query.setParameter(key.toString(), map.get(key).toString());
//        }
